﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
using System.Text;
using System.IO;
using Aandelendelen.Db;
using Aandelendelen.Stocks;

namespace Aandelendelen.Obs
{
	public class SaldoDetails : BaseDataTable
	{
		public decimal totalCredit = 0;
		public decimal totalDebit = 0;

		public SaldoDetails(int userId)
			: base("Saldo historie")
		{
			this.csvHeader = "Datum, Bijschrijvingen, Afschrijvingen, Omschrijving, Trekking";

			this.Columns.Add(new DataColumn("Datum", typeof(string)));
			this.Columns.Add(new DataColumn("Credit", typeof(string)));
			this.Columns.Add(new DataColumn("Debet", typeof(string)));
			this.Columns.Add(new DataColumn("Description", typeof(string)));
			this.Columns.Add(new DataColumn("Fonds", typeof(string)));

			Query query = new Query("s_saldodetails", "Id", userId, OleDbType.Integer);

			ResultSet resultSet = (ResultSet)query.Execute();

			this.AddDetails(resultSet);
		}

		public decimal TotalCredit
		{
			get { return this.totalCredit; }
		}

		public decimal TotalDebit
		{
			get { return this.totalDebit; }
		}

		private void AddDetails(ResultSet resultSet)
		{
			this.totalCredit = 0;
			this.totalDebit = 0;

			decimal refAmount = 0;
			bool refRowOpen = false;
			DataRow refRow = null;
			int numRows = resultSet.Count;

			for (int i = 0; i < numRows; i++)
			{
				DateTime date = (DateTime)resultSet[i, "Datum"];
				string strDate = date.ToString("dd-MM-yyyy, HH:mm:ss");
				int type = (int)resultSet[i, "Type"];
				int fonds = (int)resultSet[i, "Fonds"];
				string sFonds = StockIDCollection.GetInstance().StocksById[fonds];

				string amount = "";

				object oAmount = resultSet[i, "Bedrag"];
				if (oAmount != null)
				{
					decimal dAmount = (decimal)oAmount;
/*
					if (type == 5)
					{
						if (trekking == refTrekking)
							refAmount += dAmount;
						else
						{
							if (refRowOpen)
								refRow[1] = ObjectFormatter.GetCurrencyString(refAmount);

							refRowOpen = true;

							refRow = this.NewRow();
							refRow[0] = strDate;
							refRow[2] = "";
							refRow[3] = DataCache.MutationDescription[type.ToString()];
							refRow[4] = trekkingDate;

							this.Rows.Add(refRow);

							refAmount = dAmount;
							refTrekking = trekking;
						}
					}
*/

					if (dAmount >= 0)
						this.totalCredit += dAmount;
					else
						this.totalDebit += dAmount;

					amount = ObjectFormatter.GetCurrencyString(dAmount);
				}

				if (type != 5)
				{
					DataRow row = this.NewRow();
					row[0] = strDate;
					row[1] = (type > 0 ? amount : "");
					row[2] = (type > 0 ? "" : amount);
					row[3] = MutationDescription.GetInstance()[type.ToString()];
					row[4] = sFonds;

					this.Rows.Add(row);
				}
			}

			if (refRowOpen)
				refRow[1] = ObjectFormatter.GetCurrencyString(refAmount);
		}
	}
}